package org.infosys.basic.listener;

import lombok.extern.slf4j.Slf4j;
import org.infosys.basic.service.IPermissionScanService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

// 表示自定义监听器，需要被spring管理
@WebListener
// 主动打印log4j日志
@Slf4j
public class PermissionScanInitListener implements ServletContextListener {

    @Autowired
    private IPermissionScanService permissionScanService;

    // 容器初始化之后被调用
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 为了防止代码复杂影响项目启动，创建线程来运行
        new Thread(() -> {
            log.info("contextInitialized start...");
            permissionScanService.scanPermission();
            log.info("contextInitialized stop...");
        }).start();
    }

    // 程序停止时调用
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        log.info("PermissionScanInitInstener stop..");
    }
}
